#include "StdAfx.h"
#include "Mesh.h"

#include "sys/File.h"

CMesh::CMesh()
{
	
}

CMesh::~CMesh()
{
	
}

BOOL CMesh::load(CFile *f)
{
	f->read(&m_header, sizeof(m_header));
	
	int n1 = sizeof(TMeshHeader);
	int n2 = sizeof(TVertex);
	int n3 = sizeof(TFace);
	
	m_verts = new TVertex[m_header.num_verts];
	f->read(m_verts, m_header.num_verts * sizeof(TVertex));
	
	m_faces = new TFace[m_header.num_faces];
	f->read(m_faces, m_header.num_faces * sizeof(TFace));
	
	m_tverts = new TCoord[m_header.num_tverts];
	f->read(m_tverts, m_header.num_tverts * sizeof(TCoord));
	
	m_tfaces = new TFace[m_header.num_faces];
	f->read(m_tfaces, m_header.num_faces * sizeof(TFace));
	
	m_normals = new TNormal[m_header.num_faces];
	f->read(m_normals, m_header.num_faces * sizeof(TNormal));

	return TRUE;
}

#define CONVERT(x) ((fx32)(x))
void CMesh::render()
{
	G3_Begin(GX_BEGIN_TRIANGLES);
	
	for (int i=0;i<m_header.num_faces;i++)
	{
		G3_TexCoord(CONVERT(m_tverts[m_tfaces[i].v1].u), CONVERT(m_tverts[m_tfaces[i].v1].v));
//		G3_Normal(m_normals[i].v1.x, m_normals[i].v1.y, m_normals[i].v1.z);
		G3_Vtx(m_verts[m_faces[i].v1].x, m_verts[m_faces[i].v1].y, m_verts[m_faces[i].v1].z);

		G3_TexCoord(CONVERT(m_tverts[m_tfaces[i].v2].u), CONVERT(m_tverts[m_tfaces[i].v2].v));
//		G3_Normal(m_normals[i].v2.x, m_normals[i].v2.y, m_normals[i].v2.z);
		G3_Vtx(m_verts[m_faces[i].v2].x, m_verts[m_faces[i].v2].y, m_verts[m_faces[i].v2].z);

		G3_TexCoord(CONVERT(m_tverts[m_tfaces[i].v3].u), CONVERT(m_tverts[m_tfaces[i].v3].v));
//		G3_Normal(m_normals[i].v3.x, m_normals[i].v3.y, m_normals[i].v3.z);
		G3_Vtx(m_verts[m_faces[i].v3].x, m_verts[m_faces[i].v3].y, m_verts[m_faces[i].v3].z);
	}

	G3_End();	
}